package com.easylive.web.task;

import com.easylive.entity.component.EsSearchComponent;
import com.easylive.entity.component.RedisComponent;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.VideoPlayInfoDTO;
import com.easylive.entity.enums.DateTimePatternEnum;
import com.easylive.entity.enums.SearchOrderTypeEnum;
import com.easylive.entity.po.VideoInfoFilePost;
import com.easylive.service.VideoInfoPostService;
import com.easylive.service.VideoInfoService;
import com.easylive.service.VideoPlayHistoryService;
import com.easylive.utils.DateUtil;
import com.easylive.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/9 21:41
 **/
@Component
@Slf4j
public class ExecuteQueueTask {

    private ExecutorService executorService = Executors.newFixedThreadPool(Constants.LENGTH_2);

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private VideoInfoPostService videoInfoPostService;

    @Resource
    private EsSearchComponent esSearchComponent;

    @Resource
    private VideoPlayHistoryService videoPlayHistoryService;

    /**
     * postConstruct 表示启动项目后就执行该方法
     */
    @PostConstruct
    public void consumeTransferFileQueue() {
        /**
         * 需要启动一个线程，不然就阻塞了主线程，会卡住
         */
        executorService.execute(() -> {
            while (true) {
                try {
                    VideoInfoFilePost videoInfoFilePost = redisComponent.getFileTransferQueue();
                    if (ObjectUtils.isEmpty(videoInfoFilePost)) {
//                        log.info("{} ---- 没有消息需要处理，sleep 5秒", DateUtil.format(new Date(), DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern()));
                        /**
                         * 线程休眠5秒，防止无限循环
                         */
                        Thread.sleep(5000);
                        continue;
                    }
                    videoInfoPostService.transferVideoFile(videoInfoFilePost);
                } catch (Exception e) {
                    log.error("处理文件转码异常");
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 点击播放视频后，需要做一些处理，例如更新播放次数，更新最后播放时间等等
     */
    @PostConstruct
    public void consumeVideoPlayQueue() {
        executorService.execute(() -> {
            while (true) {
                try {
                    VideoPlayInfoDTO videoPlayInfo = redisComponent.getVideoPlayInfo();
                    if (ObjectUtils.isEmpty(videoPlayInfo)) {
                        /**
                         * 线程休眠5秒，防止无限循环
                         */
                        Thread.sleep(5000);
                        continue;
                    }
                    videoInfoPostService.addReadCount(videoPlayInfo.getVideoId());
                    if (!StringTools.isEmpty(videoPlayInfo.getUserId())) {
                        // 增加用户历史记录
                        videoPlayHistoryService.saveHistory(videoPlayInfo.getUserId(), videoPlayInfo.getVideoId(), videoPlayInfo.getFileIndex());
                    }
                    // 按天记录视频播放量，后台管理使用
                    redisComponent.recordVideoPlayCount(videoPlayInfo.getVideoId());
                    // 更新ES播放数量
                    esSearchComponent.updateDocCount(videoPlayInfo.getVideoId(), SearchOrderTypeEnum.VIDEO_PLAY.getField(), Constants.ONE);
                } catch (Exception e) {
                    log.error("获取视频播放队列异常");
                    e.printStackTrace();
                }
            }
        });
    }
}
